﻿using System;
using System.Collections.Generic;
using System.Linq;
using Wicture.DbRESTFul.SQL;
using Wicture.EPC.Service.Etka.Models;
using Wicture.EPC.Service.Etka.Resources;

namespace Wicture.EPC.Service.Etka.QueryInterceptors.Interceptors
{
    public class PorscheQueryInterceptor : ETKAQueryInterceptor
    {
        private const string PorscheBrandName = "porsche";
        protected override HashSet<string> IgnoreFamilys { get; } = new HashSet<string> { "PAM" };
        public override string[] BrandCodes { get; } = new string[] { PorscheBrandName };

        protected override IEnumerable<dynamic> GetVinDetails(DbRESTFulRepository repository, Get_Model_InfoParam request)
        {
            return repository.Invoke(CSIs.Query_Vin_By_Code_For_Porsche, request);
        }

        protected override VinInfo GetVinInfo(DbRESTFulRepository repository, string vin)
        {
            return repository.Invoke<VinInfo>(CSIs.Query_vin_info_for_prosche, new { vin });
        }

        protected override bool VerifyFeatureCodes(string brandCode, List<PR> vinPRs, VinInfo vinInfo, VinFilter vinFilter)
        {
            var prCodes = vinFilter.prFilter;

            if (string.IsNullOrEmpty(prCodes))
            {
                return true;
            }

            return VerifyFeatureCodesWithNagitive(brandCode, prCodes, vinPRs, vinInfo, vinFilter.isPart);
        }

        protected override bool CheckOtherFilters(string brandCode, List<PR> PRFamilies, VinInfo vinInfo, VinFilter vinFilter)
        {
            if (!VerifyFeatureCodes(brandCode, PRFamilies, vinInfo, vinFilter))
            {
                return false;
            }

            // 处理 modelFilter (通常是transmissionCode)
            if (!string.IsNullOrEmpty(vinFilter.modelFilter))
            {
                var modelCodes = vinFilter.modelFilter.Split(",", StringSplitOptions.RemoveEmptyEntries).Select(q=>q?.Trim('$')).Distinct();
                if (modelCodes.Count() > 0)
                {
                    var allTransmissionCodes = ResourceHelper.EtkaTransmissionCodeDict[brandCode];

                    // 保时捷的变速箱配置也放在了 modelFilter 字段。
                    // 满足所有代码都是变速箱代码，且如果有不匹配的情况，就认为不属于此VIN码。
                    if (modelCodes.All(c => allTransmissionCodes.Contains(c)) && !modelCodes.Contains(vinInfo.transmissionCode))
                    {
                        return false;
                    }
                }
            }

            return true;
        }

        //protected override bool VerifyFeatureCodes(string brandCode, List<PR> vinPRs, VinInfo vinInfo, VinFilter vinFilter)
        //{
        //    var prCodes = vinFilter.prFilter;

        //    // TODO: vinFilter.modelFilter?.Contains("PR") (猜测)
        //    if (!string.IsNullOrEmpty(vinFilter.featureCodes) && (string.IsNullOrEmpty(vinFilter.modelFilter) || vinFilter.modelFilter.StartsWith("PR") == true))
        //    {
        //        prCodes = vinFilter.prFilter + "," + vinFilter.featureCodes;
        //    }

        //    if(string.IsNullOrEmpty(prCodes))
        //    {
        //        return true;
        //    }

        //    return VerifyFeatureCodesWithNagitive(brandCode, prCodes, vinPRs, vinInfo);
        //}
    }
}



